#! /usr/bin/env php
<?php
set_error_handler("exception_error_handler");
define('version','0.0.1');
if (function_exists("getopt")) {
    $arOptions = getopt("i::p::hv",array('ip::','port::','help','version'));
    if (isset($arOptions['h'])||isset($arOptions['help'])) {
        print "Usage: node.php [options]\nOptions:\n--ip=IPADDRESS  or      -i=IPADDRESS     Overrides listen ip from config\n--port=PORT     or      -p=PORT          Overrides listen port from config\n--help          or      -h               Display this help\n--version       or      -v               Display version of Node.php\n";
        exit();
    }
    if (isset($arOptions['v'])||isset($arOptions['version'])) {
        print "Node.php " . version ."\n";
        exit();
    }
    $sIp =      config::getInstance()->get('ip');
    $iPort =    config::getInstance()->get('port');
    if (isset($arOptions['i'])) {
        if (is_array($arOptions['i'])) {
            $sIp = $arOptions['i'][0];
        } else {
            $sIp = $arOptions['i'];
        }
    }
    if (isset($arOptions['ip'])) {
        if (is_array($arOptions['ip'])) {
            $sIp = $arOptions['ip'][0];
        } else {
            $sIp = $arOptions['ip'];
        }
    }
    if (isset($arOptions['p'])) {
        if (is_array($arOptions['p'])) {
            $iPort = $arOptions['p'][0];
        } else {
            $iPort = $arOptions['p'];
        }
    }
    if (isset($arOptions['port'])) {
        if (is_array($arOptions['port'])) {
            $iPort = $arOptions['port'][0];
        } else {
            $iPort = $arOptions['port'];
        }
    }
    config::getInstance()->set('ip',$sIp)->set('port',$iPort);


} else {
    //Походу винда... Надо будет найти еще способ обрабатывать параметры...
}
if (config::getInstance()->get('db')=='simpledb') {
    simpledb::getInstance();
}
log::raise("Start parsing templates...");
tpl::getInstance()->parseTemplates();
log::raise("Complete parsing templates...");

$arModules = glob('./apps/*.class.php');
foreach ($arModules as $sModule) {
    $sName = str_replace('./apps/','',$sModule);
    $sName = str_replace('.class.php','',$sName);
    $sObjectName = 'ob'.strtoupper(substr($sName, 0,1)) . substr($sName, 1);
    include_once $sModule;
    $$sObjectName = new $sName;
    log::raise("$sObjectName object from $sModule");
}


server(array(       'timeout' => config::getInstance()->get('timeout'),
                    'request' => 'incommingRequest',
                    'single' => true)
)->listen(config::getInstance()->get('ip'),
          config::getInstance()->get('port'));

function incommingRequest($obHTTP) {
   router::route($obHTTP);
}


function server($arParams) {
    return new server($arParams);
}

function streamserver($arParams) {
    return new streamserver($arParams);
}

function __autoload($sClass) {
    if (file_exists("./system/$sClass.class.php")) {
        include "./system/$sClass.class.php";
        return true;
    } else {
        return false;
    }
}

function exception_error_handler($errno, $errstr, $errfile, $errline ) {
    log::raise("$errstr in $errfile:$errline");
}

